package com.huluobo.a2307douyindemo.demo.lifeCycle

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.huluobo.a2307douyindemo.R

/**
 *  Created by LC on 2025/9/12.
 */
class TipDialog(context: Context) : Dialog(context), LifecycleObserver {
    init {
        //判断如果上下文是个Activity的话,而且必须得是实现了被观察者接口的Activity,自动产生订阅关系
        if (context is AppCompatActivity) {
            //产生订阅关系
            context.lifecycle.addObserver(this)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    private fun onShow() {
        //当感知到activity的生命周期是创建的时候,展示dialog
        show()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    private fun onDismiss() {
        //当感知到activity的生命周期是销毁的时候,同时判断dialog是展示的状态
        //关闭掉dialog
        if (isShowing) {
            dismiss()
        }
    }
}